﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace HYCoordinate
{
	public class ChamferCornerCurveMaker : IMakeCornerCurves, ICheckAngle
	{
		private ChamferCornerCurveMaker()
		{
		}

		public static ChamferCornerCurveMaker GetSingleChamferCornerCurveMaker()
		{
			if (ChamferCornerCurveMaker.singleTag == null)
			{
				ChamferCornerCurveMaker.singleTag = new ChamferCornerCurveMaker();
			}
			return ChamferCornerCurveMaker.singleTag;
		}

		public List<Curve> GetCornerBoundryCurves(ThreeInputDataPacker input, out XYZ startPoint_Mid, out XYZ endPoint_Mind)
		{
			new List<Curve>();
			List<Curve> list;
			List<Curve> list2;
			Utiltiy.GetInnerIntersectionPoint(input.GetTwoInputData(0), input.GetTwoInputData(1), out list, out list2);
			XYZ xyz;
			XYZ xyz2;
			XYZ xyzpoint;
			XYZ xyzpoint2;
			XYZ xyz3;
			XYZ xyz4;
			XYZ item;
			XYZ item2;
			if (!Utiltiy.GetCornernRectangle(input, out xyz, out xyz2, out xyzpoint, out xyzpoint2))
			{
				xyz3 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyz4 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				item = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				item2 = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list[1].Project(xyz3).XYZPoint;
				xyzpoint2 = list2[1].Project(xyz4).XYZPoint;
			}
			else if (input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Width] > input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Width])
			{
				xyz3 = xyzpoint.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list[1].Project(xyz3).XYZPoint;
				xyz4 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint2 = list2[1].Project(xyz4).XYZPoint;
				item = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				item2 = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
			}
			else
			{
				if (input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Width] >= input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Width])
				{
					throw new CornerMakerException(StaticRescource.StrDontWantIf);
				}
				xyz4 = xyzpoint2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint2 = list2[1].Project(xyz4).XYZPoint;
				xyz3 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list[1].Project(xyz3).XYZPoint;
				item = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				item2 = xyz2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
			}
			startPoint_Mid = xyz3.Add(xyzpoint).Divide(2.0);
			endPoint_Mind = xyz4.Add(xyzpoint2).Divide(2.0);
			List<Curve> lstBoundLineCurve = Utiltiy.GetLstBoundLineCurve(new List<XYZ>
			{
				xyz3,
				xyzpoint,
				item,
				item2,
				xyzpoint2,
				xyz4
			});
			if (Utiltiy.CheckNoneEndInterintersection(lstBoundLineCurve))
			{
				throw new CornerMakerException(StaticRescource.StrError_cornerLineIntersection);
			}
			return lstBoundLineCurve;
		}

		public bool IfAngleCanUse(ThreeInputDataPacker input)
		{
			throw new NotImplementedException();
		}

		private static ChamferCornerCurveMaker singleTag;
	}
}
